#!/usr/bin/env node

const http = require('http'),
    mvlist = require('./data.js'),
        fs = require('fs');

let arr = [];
let obj = {};

function getPage() {
  let html = fs.readFileSync('./index.html').toString('utf8');
  return html;
}

function getData() {
  mvlist.map(item => {
    if (item.movieRange !== '暂无评分') {
      arr.push(item);
    }
  })
  for (var i = 0; i < arr.length; i++) {
    for (var j = i; j < arr.length; j++) {
      if (arr[i].movieRange < arr[j].movieRange) {
        obj = arr[i];
        arr[i] = arr[j];
        arr[j] = obj;
      }
    }
  }
  return arr;
}

http.createServer((req, res) => {

  if (req.url === '/') {
    res.end(getPage());
  } else if (req.url === '/getlist') {
    res.end(JSON.stringify(getData()));
  }

}).listen(8080);


